#!/bin/bash

fitfile=$1
if [ $# -ne 1  ]
then
	echo "usage[ xgft_ot.sh a.fit ]"
	exit 0
fi

DeltaTime_p=240 #sec, the delay time from date time to the trigger time in sec. The first data products to be generated. 
DelayTime_UpdateSP=10  #1800 #seconds to update the science products
scaleH=0.66
scaleL=0.50
errorbar=60 #arcsec
Nobj_least=3  # it is the least obj in the fitfile
SolveRadius=0.3  # degree, for solve-field when doing the AStronomy
SolveRadiusT=1  # degree, for solve-field when doing the AStronomy_2
Ncompstar_c=3  # the number of comparison stars should be more than this number
ObjEllipForAstro_c=0.50 #The critical value for the average ellipticity for objects in the image. if the value is larger than this value, the image is thought to be bad and this exit this run.
#serchError=44   #25arc,  #120 #it is the dirmeter, this value is the same as one in xcross60cmOT.c
serchError=120   #25arc,  #120 #it is the dirmeter, this value is the same as one in xcross60cmOT.c
gain=2.0   #have to be tested more
fluxmax_adu=50000  #adu
Dir_result=/disk2/test/xlp2/result/
Dir_candidate=/disk2/test/xlp2/candidate/
SPTimelog=/disk2/test/xlp2/result/NMktime.log

xcopy ( )
{

echo "===========xcopy============"
Dir_PS1catalog=/disk2/process/PS1catalog
if test ! -d $Dir_PS1catalog
then
	mkdir $Dir_PS1catalog
	echo "$Dir_PS1catalog have set up"
fi

if test -r reduc
then
	rm -rf reduc
fi
mkdir reduc
cp $fitfile reduc
cd reduc
Dir_now=`pwd`
cp ~/software/softgft/* ./
}


xSend2db ( )
{
#This step shall be set after the function of xGetkeywords_1.
#to send the process message to GFT DB
echo "xSend2db"
#obs_id="1001"
time_utc_process=`date -u +%F"T"%H":"%M":"%S`
event=`echo ${@:1} | awk '{print($0)}'`
if [ $filter == "g" ]
then
	detector_id=1
elif  [ $filter == "r" ]
then
	 detector_id=2
else
	 detector_id=3
fi

#curl -d img_name=$fitfile -d event="over" -d error_code=0 -d time_utc=$dateobs -d filter=$filter -d detector_id=$detector_id -d obs_id=$obs_id -d pipeline=gftpp -X POST http://192.168.199.130:8080/cgft/addProStatus.action
echo "curl -d img_name=$fitfile -d event=$event -d error_code=0 -d time_utc=$dateobs -d filter=$filter -d detector_id=$detector_id -d obs_id=$obs_id -d pipeline=gftpp -X POST http://192.168.199.130:8080/cgft/addProStatus.action"
curl -d img_name=$fitfile -d event="$event" -d error_code=0 -d time_utc=$time_utc_process -d filter=$filter -d detector_id=$detector_id -d obs_id=$obs_id -d pipeline=gftpp -X POST http://192.168.199.130:8080/cgft/addProStatus.action
}



xGetkeywords_1 (  )
{
    echo `pwd`
    echo "xGetkeywords_1"
    dateobs=`gethead $fitfile "date-obs"` #2018-09-15T11:35:34.009
    echo "date-obs="$dateobs
    #OTid=`gethead $fitfile "object"`
    OTid=`gethead $fitfile "BURST_ID"`
    #Tid_Follow=`gethead $fitfile "RUN_TIME"`
    OTsource=`echo $OTid | cut -c1-1`
    exptime=`gethead $fitfile "exptime" | awk '{printf("%.0f\n",$1)}'`
    NAXIS1=`gethead $fitfile "NAXIS1" | awk '{print($1)}'`
    NAXIS1_half=`gethead $fitfile "NAXIS1" | awk '{print($1/2)}'`
    NAXIS2=`gethead $fitfile "NAXIS2" | awk '{print($1)}'`
    NAXIS2_half=`gethead $fitfile "NAXIS2" | awk '{print($1/2)}'`
    filter=`gethead $fitfile "filter"` #default is R filter right now
    JD=`gethead $fitfile "jd"`
    deltayear=`echo $JD | awk '{print(($1-2451545.0)/365.25)}'`
    CCDTYPE=`gethead $fitfile "ccdtype"` #BIAS, DARK, FLAT, LIGHT, FOCUS
    CCDTYPE="LIGHT"  
    FTid=`gethead $fitfile "inst_ba" `    #follow-up telescope id
    #FTid=F01  #west 60cm telescope is F01 and east 60cm isn F02
    #FTid="CGFT"
    if [ "$FTid"x == "F60B"x   ]
    then
	FTid="GWAC-F60B"
    fi
    if [ "$FTid"x == "F60A"x   ]
    then
	FTid="GWAC-F60A"
    fi
    telescope=`gethead $fitfile "INST_BA"` 
    if [ "$telescope"x == "F60B"x   ]
    then
	telescope="GWAC-F60B"
    fi
    if [ "$telescope"x == "F60A"x   ]
    then
	telescope="GWAC-F60A"
    fi
    
    telescopesvom=`gethead $fitfile "telescop"`
    instrument=`gethead $fitfile "instrume"`
    obstype=`gethead $fitfile "obs_type"`
    #obstype="GRB-INIT"   
    alt_id=`gethead $fitfile "alt_id"`
    #alt_id="T001"
    trig_name=`gethead $fitfile "trig_nam"`
    #trig_name="T001"
    tilenum=`gethead $fitfile "tilenum"`
    tilenum=0
    candidateID="null"
    requistID="null"
    obs_id=`gethead $fitfile "obs_id" `
#==========================================================================
    #GRB information
    TriggerID=`gethead $fitfile "BURST_ID"`     #958144
    Tzero=`gethead $fitfile "TRIG_UTC"`             #2018-09-15T11:35:34.009
    Detector=`gethead $fitfile "TRIG_DEV"`   #SVOM-Eclair
    RATTa=`gethead $fitfile "TRIG_RA"`            #16:43:48.220
    DECTTa=`gethead $fitfile "TRIG_DEC"`          #+41:02:43.40
    errorp=`gethead $fitfile "TRIG_ERR"`           #50 with the unit of  arcmin
    errorp=400.0
    triggerJD=`gethead $fitfile "TRIG_JD"`
    obsid=`gethead $fitfile "OBS_ID"`
    #obsid="T001"
    RATT=`skycoor -d $RATTa $DECTTa | awk '{print($1)}'`
    DECTT=`skycoor -d $RATTa $DECTTa | awk '{print($2)}'`
	echo $TriggerID $Tzero $Detector $RATT $DECTT $errorp $JD $triggerJD
	echo $dateobs
    #=======================================================================
    #xiaomeng  provides a c code named as str2jd.c to transform the datestring to JD
    #usage:
   	 #./str2jd $dateobs  
   	 #output are 
   	 #MJD = 58376.483032407
   	 #JD = 2458376.983032407
    #./str2jd $Tzero >tmp1
    #JDTzero=`tail -1 tmp1 | awk '{print($3)}'`
	#echo "JDTzero="$JDTzero
    #rm -rf tmp1
    JDTzero=`echo $triggerJD | awk '{print($1)}'`
    DelTimeSec=`echo $JD $JDTzero | awk '{print(($1-$2)*24*3600)}'`   # The delay time in seconds
    echo "DelTimeSec=$DelTimeSec seconds "
    #====================================================================== 
    
    gethead $fitfile "Ncombine" >tmp
    if test -s tmp
    then
       Ncombine=`cat tmp | awk '{print($1)}'`
       gethead $fitfile "Nexptime" >tmp1
       if test -s tmp1
	then
		Nexptime=`cat tmp1 | awk '{print($1)}'`
	else
		Nexptime=1
	fi
   else
 	Ncombine=1
	Nexptime=`echo $exptime | awk '{print($1)}'`
    fi
    rm -rf tmp tmp1
   echo "Ncombine=$Ncombine   Nexptime=$Nexptime"
   
   if test ! -d $Dir_result"/"$telescope
   then
           mkdir $Dir_result"/"$telescope
   fi
   if test ! -d $Dir_candidate"/"$telescope
   then
           mkdir $Dir_candidate"/"$telescope
   fi

   Dir_result=`echo $Dir_result"/"$telescope"/"$filter | awk '{print($1)}'`
   Dir_candidate=`echo $Dir_candidate"/"$telescope"/"$filter | awk '{print($1)}'`
   if test ! -d $Dir_result
   then
           mkdir $Dir_result
   fi
   if test ! -d $Dir_candidate
   then
           mkdir $Dir_candidate
   fi	
   echo $Dir_candidate 
   #Dir_result=`echo $Dir_result"/"$filter | awk '{print($1)}'`
   #Dir_candidate=`echo $Dir_candidate"/"$filter | awk '{print($1)}'`
   #if test ! -d $Dir_result
   #then
   #	   mkdir $Dir_result
   #fi
   #if test ! -d $Dir_candidate
   #then
   #	   mkdir $Dir_candidate
   #fi
}


xcutimg ( )
{
#input file is $OTfile

#have to name each candidate

echo "=====xcutimg======"

Dir_now=`pwd`
aa=`echo $Dir_now | awk '{print($1"/")}'`
Dir_now=`echo $aa | awk '{print($1)}'`
wc $OTfile
echo "==============="
cat $OTfile
echo "==============="
cat $OTfile | while read line2
do
	echo $line2
	sourcename=`echo $line2 | awk '{print($1)}'`
	imsubname=`echo $sourcename $TriggerID $prefix | awk '{print($3"_"$1"_cut.fit")}'`
	ccdimgjpg=`echo $imsubname | sed 's/\.fit/.png/g'`
	echo "sourcename="$sourcename
	echo "imsubname="$imsubname
	echo "ccdimgjpg="$ccdimgjpg
	xc=`echo $line2 | awk '{print($3)}'`   #have to check the format of $OTfile
	yc=`echo $line2 | awk '{print($4)}'`   #have to check the format of $OTfile
	rac=`echo $line2 | awk '{print($8)}'`
	decc=`echo $line2 | awk '{print($9)}'`
	if test -r $imsubname
        then
                rm $imsubname
        fi
	boxpixel=50
	echo $xc $yc
	xc1=50
	yc1=50
        xmin=`echo $xc $boxpixel | awk '{print($1-$2)}'`
        xmax=`echo $xc $boxpixel | awk '{print($1+$2)}'`
        ymin=`echo $yc $boxpixel | awk '{print($1-$2)}'`
        ymax=`echo $yc $boxpixel | awk '{print($1+$2)}'`
	if [ ` echo " $xmin < 0 " | bc ` -eq 1  ] 
	then
		xc1=`echo $xmin | awk '{print(50+$1)}'`
		xmin=0
	fi
	if [ ` echo " $ymin < 0 " | bc ` -eq 1  ]
        then
		yc1=`echo $ymin | awk '{print(50+$1)}'`
                ymin=0
        fi
	if [ ` echo " $xmax > 2048 " | bc ` -eq 1  ]
        then
                xmax=2048
        fi
	if [ ` echo " $ymax > 2048 " | bc ` -eq 1  ]
        then
                ymax=2048
        fi


	#echo "~/anaconda3/bin/python /home/cgft/software/xsoft/newxgwacmatchsoft/get_sub_fits_xy.py $Dir_now  $fitfile $Dir_now $imsubname $xmin $xmax $ymin $ymax"
	#/home/cgft/anaconda3/bin/python /home/cgft/software/xsoft/newxgwacmatchsoft/get_sub_fits_xy.py $Dir_now  $fitfile $Dir_now $imsubname $xmin $xmax $ymin $ymax
	#wait
	# to convert the fit image to jpg file
#	/home/cgft/anaconda3/bin/python ~/software/softgft/xReadFitsWithWCS.py $fitfile 
#	/home/cgft/anaconda3/bin/python  ~/software/xsoft/newxgwacmatchsoft/fits_cut_to_png.py $fitfile $ccdimgjpg 50 50 50 ""
#	convert -strip -interlace Plane -gaussian-blur 0.01 -quality 60% $ccdimgjpg temp.jpg
#	mv tmp.jpg $ccdimgjpg
	echo $fitfile $xmin $xmax $ymin $ymax $sourcename $xc $yc $xc1 $yc1>>cutlog 
	/home/cgft/anaconda3/bin/python ~/software/softgft/xReadFitsWithWCS2_1.py $fitfile $xmin $xmax $ymin $ymax $sourcename $rac $decc
	wait
	#output is $imsubname  and $ccdimgjpg
	#get ps1 image
	#if test -r PS1.png
	#then
	#	rm -rf PS1.png
	#fi
	#ps1size=120   #pixel
	#/home/cgft/anaconda3/bin/python ~/software/softgft/getPs1png.py $rac $decc $ps1size 
	#wait
	##paste cutimage and ps1 image to combined image
	##and then rename to $ccdimgjpg
	#/home/cgft/anaconda3/bin/python ~/software/softgft/p2png.py $ccdimgjpg PS1.png $ccdimgjpg
	#python3  ~/software/softgft/sendFile.py  cutimagepng  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $ccdimgjpg
	#python3  ~/software/softgft/sendFile.py  cutimagefit  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $imsubname
#	#curl $UploadParameterfile -F fileUpload=@$ccdimgjpg
#	#echo $imsubname $ccdimgjpg 
	#echo "xcutimg is over"
	##rm -rf $ccdimgjpg
	
done
}

xGetkeywords ( )
{ 
    echo "=======xGetkeywords========"
    RAT=`xy2sky -d $fitfile $NAXIS1_half $NAXIS1_half | awk '{print($1)}'`
    DECT=`xy2sky -d $fitfile $NAXIS2_half $NAXIS2_half | awk '{print($2)}'`
    #echo "==============="
    #echo "OTid_Follow=$OTid_Follow ,  OTid=$OTid"
    #OTid=`echo $OTid_Follow | cut -c1-14`
    #echo "RAT=$RAT DECT=$DECT" 
    RA=`skycoor -d $RAT $DECT | awk '{print($1)}'`
    DEC=`skycoor -d $RAT $DECT | awk '{print($2)}'`
    dis_pointerror=`skycoor -r $RA $DEC $RATT $DECTT | awk '{print($1)}'`   # pointing error in arcsec.
	echo "dis_pointerror=$dis_pointerror arcsec"
    echo $dateobs $OTid $FTid $exptime $filter $RA $DEC $JD $CCDTYPE   $dis_pointerror
    echo $RA $DEC 
    dis_PS1error=`echo $errorp | awk '{print($1+120)}'`   #to get the PS1 catalog with an radius which is more larger than errorp by 120 arcsec, where errorp is the error radius from the alert.

   CCDsize=`gethead $fitfile "NAXIS1" | awk '{print($1)}'`
   #CCDsize=1024.0 # for 60cm
   #CCDsize=2048.0  #for 50cm
   CCDcenter=`echo $CCDsize | awk '{print($1/2)}'`
   ejmin=20.0
   ejmax=`echo $CCDsize $ejmin | awk '{print($1-$2)}'`
}

xUSNOcross ( )
{

echo "xUSNOcross"
}

xfilterpsf_OT ( )
{
	echo "==========xfilterpsf_OT============="

       Dir_process=`pwd`
       PSF_Critical_min=2.2
       PSF_Critical_max=10.0
       cat newOT.txt | awk '{print($1,$2, "  1 a")}' >psf.dat
       #cat psf.dat
       cd $HOME/iraf
       cp -f login.cl.old login.cl
       echo noao >> login.cl
       echo image >> login.cl
       echo digiphot >> login.cl
       echo daophot >>login.cl
       echo "cd $Dir_process" >> login.cl
       #echo "imexam(input=\"$fitfile\", imagecu=\"psf.dat\")" >>login.cl       
       echo "daoedit(\"$fitfile\", icommand=\"psf.dat\")"  >> login.cl
       echo logout >> login.cl
       cl < login.cl  >OUTPUT_PSF
       #cl < login.cl
        mv OUTPUT_PSF $Dir_process
       cp -f login.cl.old login.cl
       cd $Dir_process
       cat OUTPUT_PSF | grep "ERROR" >errormsg
       if test ! -s errormsg
       then
           #cp OUTPUT_PSF $psffile
           cat OUTPUT_PSF | sed -e '/^$/d' | grep '[1-9]' | grep -v "NOAO" | grep -v "This" | grep -v "line" | grep -v "m" >OUTPUT_PSF1
           paste OUTPUT_PSF1 newOT.txt | awk '{if($5>PSF_Critical_min && $5<PSF_Critical_max)print($8,$9,$10,$11,$5,$12)}' PSF_Critical_min=$PSF_Critical_min PSF_Critical_max=$PSF_Critical_max >temp
           mv temp newOT.txt
       else
           echo "Error in psf checking for OT candidates"
       fi

}

xmklcparameter( )
{
	echo "xmklcparameter"
	echo $LcForObject
	echo $sourcename
	Ndetection=`wc $LcForObject | awk '{print($1)}'`
if [ ` echo " $Ndetection > 1 " | bc ` -eq 1  ] 
then
	mag1=`sort -n -r -k 5 $LcForObject | head -1 | awk '{print($5)}'`
	mag2=`sort -n -r -k 5 $LcForObject | tail -1 | awk '{print($5)}'`
	amp=`echo $mag1 $mag2 | awk '{print($1-$2)}'`
	qti=0
	echo "mag1=$mag1, mag2=$mag2, amp=$amp qti=$qti"

	echo $TriggerID
	#candidateID="null"
        #requistID="null"
else
	qti=0
	amp=0
fi
if [ ` echo " $Ndetection > 2 " | bc ` -eq 1  ]
then

	lcpng=`echo $LcForObject | awk '{print($1".png")}'`
gnuplot <<EOF
set term png
set output "$lcpng"
f(x)=2.5*a*log(x)/log(10)+b
fit f(x) '$LcForObject' u 1:5 via a,b
set xlabel "Seconds from the trigger time"
set ylabel "magnitude"
set yrange[] reverse
set grid
plot '$LcForObject' u 1:5 t 'data', f(x) t 'fit'
EOF
qti=`tail -7 fit.log | head -1 | awk '{print($3)}'`
fi


#	lcparam=`echo $TriggerID".lcp"`
cat $lcparam | grep -v $sourcename >tmp
mv tmp $lcparam
echo $sourcename $amp $qti $Ndetection $telescope $obsid $TriggerID $candidateID $requistID >>$lcparam
echo "========$lcparam======"
cat $lcparam
echo "======================"

}

xMkLcForEachObject ( )
{
	echo "xMkLcForEachObject"
        xMkDirForTriggerID	
	cp $Dir_candidate"/"$GRBcandidatelog ./
	if test -s $GRBcandidatelog
	then
		echo "have $GRBcandidatelog"
		cat $GRBcandidatelog | while read lineobjname
		do
			sourcename=`echo $lineobjname | awk '{print($1)}'`
			cat $OTfile | grep $sourcename >aa
			if test -s aa
			then
				LcForObject=`echo $Dir_result_trigger $sourcename | awk '{print($1"/"$2".lc")}'`
				lcparam=`echo $Dir_result_trigger $TriggerID | awk '{print($1"/"$2".lcp")}'`
				echo $LcForObject
				if test -r $LcForObject
				then
					cat $LcForObject aa >>bb
				else
					cat aa >>bb
				fi
				mv bb $LcForObject 
				rm aa
				xmklcparameter
			fi
		done

	else
		echo "no $GRBcandidatelog"
	fi

}


xNameCandidate ( )
{
	echo "xNameCandidate"
	#to give a name for each candidate
	#input is $OTfile
	#output is $GRBcandidatefile
	#name is JRA+DEC
	#one source in several files shall be consistent with the criterial 
	cp $Dir_candidate"/"$GRBcandidatelog ./
	if test -s $GRBcandidatelog
	then
		k1=0
		k2=0
		rm -rf disr12.txt
		echo "have $GRBcandidatelog"
		#sourcename  RA  DEC ...
		cat $GRBcandidatelog | while read line1
		do
			filter_a=`echo $line1 | awk '{print($6)}'`
			if [ "$filter_a"x == "$filter"x   ]
			then
				#echo $filter_a $filter
				k2=`echo $k1 | awk '{print($1+1)}'`		
				#echo $line1
				RA2=`echo $line1 | awk '{print($2)}'` 
				DEC2=`echo $line1 | awk '{print($3)}'` 
				disr12=`skycoor -r $RA1 $DEC1 $RA2 $DEC2 | awk '{print($1)}'`
				#echo $RA1 $DEC1 $RA2 $DEC2 $disr12
				if [ ` echo " $disr12 < 4.0 " | bc ` -eq 1  ]
				then
					echo "match successful"
				#	echo $disr12
					sourcename1=`echo $line1 | awk '{print($1)}'`
					echo $disr12 $sourcename1 >>disr12.txt
				fi
			fi
		done
		ls disr12.txt
		if test ! -s disr12.txt
		then
			echo 999.0 >disr12.txt   #it means that the source is not in the list of $GRBcandidatelog
			#paste disr12.txt $GRBcandidatelog | sort >tmp
			#disr12c=`cat disr12.txt | awk '{print($1)}'`	
			#echo $RA1 $DEC1 $RA2 $DEC2 $disr12c >>/disk2/test/xlp/distance.log
		fi

		disr12c=`cat disr12.txt | head -1 | awk '{print($1)}'`	
		if [ ` echo " $disr12c < 4.0 " | bc ` -eq 1  ]   # crossmatch radius is 2 arcsec
		then
			echo "match successful"
			sourcename1=`head -1 disr12.txt | awk '{print($2)}'` 
			sourcename=`echo $sourcename1`	 
			echo $sourcename $disr >>disr.txt
		else
			echo "the source is a new source, which is not in the GRB candidates"
			echo $RA1 $DEC1
			if [ ` echo " $DEC1 < 0 " | bc ` -eq 1  ]
			then
				sourcename=`echo $filter $RA1 $DEC1 $TriggerID | awk '{print($1"J"$2""$3)}'`
			else
				sourcename=`echo $filter $RA1 $DEC1 $TriggerID | awk '{print($1"J"$2"+"$3)}'`
			fi
			echo $sourcename $disr >>disr.txt
			#to update the GRBcandidatelog
			#echo $sourcename $line $RA1 $RA2 $dateobs $exptime $filter $JD $DelTimeSec $averagevalue $OTid $FTid $fitfile | column -t >>$GRBcandidatelog
			echo $sourcename $RA1 $DEC1 $dateobs $exptime $filter $JD $DelTimeSec $averagevalue $OTid $FTid $fitfile | column -t >>$GRBcandidatelog
		fi
		rm -rf tmp
		rm -rf disr12.txt
	else
		echo "no GRBcandidatelog"
		echo $RA1 $DEC1
		if [ ` echo " $DEC1 < 0 " | bc ` -eq 1  ]
		then
			sourcename=`echo $filter  $RA1 $DEC1 $TriggerID | awk '{print($1"J"$2""$3)}'`
			#sourcename=`echo $RA1 $DEC1 | awk '{print("J"$1""$2)}'`
		else
			sourcename=`echo $filter $RA1 $DEC1 $TriggerID | awk '{print($1"J"$2"+"$3)}'`
			#sourcename=`echo $RA1 $DEC1 | awk '{print("J"$1"+"$2)}'`
		fi
		echo $sourcename $disr >>disr.txt
		echo $sourcename $RA1 $DEC1 $dateobs $exptime $filter $JD $DelTimeSec $averagevalue $OTid $FTid $fitfile | column -t >>$GRBcandidatelog
		cat $GRBcandidatelog
		if test ! -d $Dir_candidate
		then
			mkdir $Dir_candidate
		fi
	fi
	cp $GRBcandidatelog $Dir_candidate
	ls $Dir_candidate

}

xcombine ( )
{
	echo "=====xcombine========"
	xstackimg.sh listcom
	wait
	if test -s newcomimage.log
	then
		echo "combine is over"
		comimage=`head -1 newcomimage.log`
		echo $comimage
		fitfile=`echo $combimage`
		xGetkeywords_1
	fi 
	


}

xcheckcombine ( )
{
echo "===xcheckcombine===="
#echo $fitfile $dateobs $DelTimeSec $JD $Nexptime $Ncombine $filter $averagevalue $RAT $DECT $RATT $DECTT $dis_pointerror $Not $timeobs_process >>$GRBlog
echo "TriggerID is $TriggerID   "
echo "DelTimeSec is $DelTimeSec  from T0 in sec"
cat $GRBlog | awk '{if($6==1 && $13<120)print($0)}' >tmpGRBlog   #1)single image; && 2)pointing error is less than 120 arcsec.
echo $GRBlog
cat $GRBlog

JD1=`head -1 tmpGRBlog | awk '{print($4)}'`
JD2=`tail -1 tmpGRBlog | awk '{print($4)}'`
DelTimeExp=`echo $JD2 $JD1 | awk '{print(($1-$2)*24*3600)}'`

echo "JD1=$JD1, JD2=$JD2, DelTimeExp=$DelTimeExp"
if [ ` echo " $DelTimeSec < $DelTimeExp " | bc ` -eq 1  ]  #
then
	echo "To combine the images"
	mv tmpGRBlog listcom
	xcombine	
else
	echo "Waiting for more images to stack, DelTimeSec=$DelTimeSec, DelTimeExp=$DelTimeExp"
fi
if test -s $GRB_Total_log
then
	cat $GRB_Total_log $GRBlog >tmp
	mv tmp $GRB_Total_log
	else
	mv $GRBlog $GRB_Total_log
fi
rm tmpGRBlog  $GRBlog

}

xImageParam( )
{
	echo "============xImageParam========="
	imgparam=`echo $fitfile | sed 's/.fit/.param/g'`
	if [ "$telescope"x == "CGFT"x   ]
	then
		fov="21arcmin"
	elif [ "$telescope"x == "GWAC-F60A"x    ]
	then
		fov="9arcmin"
	elif [ "$telescope"x == "GWAC-F60B"x   ]
	then
		fov="19arcmin"
	fi
	ccdsize=`echo $NAXIS1"x"$NAXIS2`
	echo $fitfile $limitmag $fov $ccdsize $RA $DEC $dateobs $telescope $Ncombine $filter $exptime $obsid $TriggerID $candidateID  $requistID
	echo "================="
	echo $fitfile $limitmag $fov $ccdsize $RA $DEC $dateobs $telescope $Ncombine $filter $exptime $obsid $TriggerID $candidateID $requistID >$imgparam
}

xOTInfo ( )
{
        if test -s newOT.txt
        then
		Not=`cat newOT.txt | wc -l | awk '{print($1)}'`
                echo "have new OT found after psf filter in the $fitfile"
                xy2sky -d $fitfile @newOT.txt >newOT_sky.txt
		#to calculate the distance of all new sources to the center of the burst
		rm -rf disr.txt
                paste newOT.txt newOT_sky.txt | column -t >$OTfile     # the log file for candidates for one images
		cat $OTfile
                rm -rf tmpOTfile disr.txt
                cat $OTfile | while read line
                do
			echo $line
			RA1=`echo $line | awk '{print($6)}'`    
			DEC1=`echo $line | awk '{print($7)}'`   
			GRB_RAC=169.95807219
			GRB_DECC=3.02006071
			#for simulation
			errorp_grb=300
			disr=`skycoor -r $RA1 $DEC1 $GRB_RAC $GRB_DECC | awk '{print($1)}'`  #arcseconds
			#disr=`skycoor -r $RA1 $DEC1 $RATT $DECTT | awk '{print($1)}'`  #arcseconds
			#echo "======================="
			#echo $RA1 $DEC1 $RATT $DECTT  $disr $errorp
			#echo "======================="
			#disr is the distance for this new source from the center of the alert in arcseconds.
			if [ ` echo " $disr < $errorp_grb " | bc ` -eq 1  ]	   #errorp_grb=errorp
			then
				xNameCandidate
			
                        	echo $line $dateobs $exptime $filter $JD $DelTimeSec $averagevalue $OTid $FTid $prefix $tilenum | column -t >>tmpOTfile
		#	cat tmp
			fi
                done
		echo "=======disr.txt====="
		#cat disr.txt
		echo "=======tmpOTfile====="
		#cat tmpOTfile
		if test -s disr.txt 
		then
        	        paste disr.txt tmpOTfile >$OTfile 
			echo "============"
			echo $OTfile
			#cat $OTfile
			#to cut the image windows around the OT position
        	        #CGFT image has 3 axis, can not work well for the xcutimg.
			#waiting for the modification of the fits by Xiaomeng.
			xcutimg   #to cut the image for each source in $OTfile
			echo "$OTfile"
			#rJ169.76656+3.00511 394.213	1076.082  1665.953  14.4688  0.029  2589.922  169.76656  3.00511  J2000  1076.082  1665.953  2024-01-17T13:03:00  10  r  2460327.04375  180  18.5123  sb24011700  CGFT  OTsb22012454_20240117_r_001  0
			#cat $OTfile | awk '{print($18,$15,$14,$16,$5,$6,$19,$9,$10,$3,$4,$2, $1, $20, $23 ,$22,$21)}' >tmpotfile
			cat $OTfile | awk '{print($17,$14,$13,$15,$5,$6,$18,$8,$9,$3,$4,$2, $1, $19, $22 ,$21,$20)}' >tmpotfile

			#18.5123 r 10 2460327.04375 14.4688 0.029 sb24011700 3.00511 J2000 1076.082 1665.953 394.213 rJ169.76656+3.00511 CGFT  0 OTsb22012454_20240117_r_001
			echo "@@@@@@@@@@@@@@@@@@"
			cat $OTfile
			#cp tmpotfile $OTfile_BAtools
			#DeltaT_sec, exptime_sec, ObsTime, filter, magnitude, merr, limitmag, RA(J2000),DEC(J2000), X, Y, Distance_to_alertPosition, OTname, triggername, tilenum(default=0), prefix, telescope 
			mv tmpotfile $OTfile
			
			echo "========================="

			xupdateForSciProducts  $Not
			echo "========================="

			xMkLcForEachObject
			echo $dateobs $DelTimeSec $OTid $FTid $exptime $filter $RA $DEC $JD $CCDTYPE   $dis_pointerror  $Not >$otlog	# the log file for data processing	
		else
        		xMkDirForTriggerID	
			 echo "There is no any transient in this image"
			 echo "no any new OT found in the $fitfile after the distance filter"
			 Not=0
			 xupdateForSciProducts $Not
		fi
        else
        	xMkDirForTriggerID	
                echo "no any new OT found in the $fitfile after the psf filter"
#                echo "The limit mag is $LIMITMAG"
        	Not=0
		xupdateForSciProducts $Not
	fi
		
	timeobs_process=`date -u +%Y%m%d%H%M%S`
	echo $fitfile $dateobs $DelTimeSec $JD  $Nexptime $Ncombine $filter $averagevalue $RAT $DECT $RATT $DECTT $dis_pointerror $Not $timeobs_process >>$GRBlog   # totoal log for GRB related images
	echo $GRBlog
	#blocked by xlp at 20210915
	#xcheckcombine
	# this file is used for the image stacking.
	
	#============================
	#for BA tools
	xsendBAtools

}

xsendBAtools ( )
{

	echo "==================for BA tools===="
	if test -s $OTfile_BAtools
	then
		# trigger_image.lc
		objflux=999
		bgflux=999
		fwhm=4.0
		ellip=2.3
		#candidateID="null"
		#requistID="null"
		lctable=`echo $TriggerID"_"$fitfile".lc"`
		#head -1 $OTfile
		#DeltaT_sec, exptime_sec, ObsTime, filter, magnitude, merr, limitmag, RA(J2000),DEC(J2000), X, Y, Distance_to_alertPosition, OTname, triggername, tilenum(default=0), prefix, telescope
		cat $OTfile_BAtools | grep -v "#" | awk '{print($13,$8,$9,"1.0", $5,$6, fitname,$10,$11, objflux, bgflux, fwhm, ellip ,$18,$1,$17, ncombine, $4, $2, $3, obsid, $14, candidateID, requistID)}' ncombine=$Ncombine objflux=$objflux bgflux=$bgflux fwhm=$fwhm  ellip=$ellip candidateID=$candidateID requistID=$requistID fitname=$fitfile obsid=$obsid >$lctable
	fi
	#============================
	xImageParam
	wait
	echo $imgparam
	echo $lctable
	python3  ~/software/softgft/sendFile.py  imageparam  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $imgparam
	wait
	msg=`echo $imgparam"_has_uploaded"`
	#xSend2db $msg
	python3  ~/software/softgft/sendFile.py  lightcurve  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $lctable
	wait
	msg=`echo $lctable"_has_uploaded"`
	#xSend2db $msg

	#triggerID.lcp when 5 min after the burst
	#echo "====To make a trigger.lcp with light curves for all transients====="
	python3  ~/software/softgft/sendFile.py  lightcurveparam  $TriggerID  001 002 $telescope  $dateobs  $telescope   pipeline  01 test1 $lcparam 
	msg=`echo $lcparam"_has_uploaded"`
	#xSend2db $msg

	echo "upload the subimage.fit and the sub.png"


	DelTimeSec_c=100 #sec
#	if [ ` echo " $DelTimeSec > $DelTimeSec_c " | bc ` -eq 1  ]
#	then
#		#echo "====To make a trigger.lcp with light curves for all transients====="
#		python3  ~/software/softgft/sendFile.py  lightcurveparam  $TriggerID  001 002 $telescope  $dateobs  $telescope   pipeline  01 test1 $lcparam 
#		msg=`echo $lcparam"_has_uploaded"`
#		#xSend2db $msg
#	fi


}

xsendCSC ( )
{
mkfchartB=`pwd`
Dir_Fchart="/disk2/pubweb/fchart"
Dir_txt="/disk2/test/simuData/products/sendCSC/BATrain/"
if test ! -d $Dir_Fchart
then
	mkdir $Dir_Fchart
fi
cd $Dir_Fchart
#Trigger_id_a="test"
Trigger_id_a=`echo $TriggerID`
#Trigger_id_a=`echo $TriggerID`
Dir_burstid=`echo $Dir_Fchart $Trigger_id_a | awk '{print($1"/"$2)}'`
Dir_burstid_res=`echo $Dir_txt $Trigger_id_a | awk '{print($1"/"$2)}'`
#Dir_burstid_res=`echo $Dir_Fchart $Trigger_id_a | awk '{print($1"/"$2"_sc1")}'`
if test ! -d $Dir_burstid
then
	mkdir $Dir_burstid
fi
if test ! -d $Dir_burstid_res
then
	mkdir $Dir_burstid_res
fi
echo $Dir_burstid
echo $Dir_burstid_res

cd $mkfchartB
echo $mkfchartB
#ls *.png
echo $OTfile
cp *forCSC.txt $Dir_burstid_res
#cp  $OTfile *fit *png *.lc *.lcp *.param $Dir_burstid_res
#cp *_cut.png *_cut.fit $OTfile $Dir_burstid
#cp *_cut.png *_cut.fit $Dir_burstid_res
cp *_cut.png  $Dir_burstid

cd /disk2/pubweb/
chmod 755 * -R 
cd $mkfchartB


}

xupdateForSciProducts ( )
{
	echo "xupdateForSciProducts"
	otnum=$1
	echo "dateobs=$dateobs"
	echo "otnum="$otnum
	if [ ` echo " $otnum > 0 " | bc ` -eq 1  ]
	then
		echo "================have ot with $otnum =========="
		echo "#- delimiter is = , split parameter with value. " >scienceProducts.txt
		curTime=`date -u +%F"T"%T`
		echo "#CREATTIME="$curTime >>scienceProducts.txt
		echo "#FINDING=True" >>scienceProducts.txt
		echo "#UPMAG=None" >>scienceProducts.txt
		echo "#OBSTYPE="$obstype >>scienceProducts.txt
		echo "#BURST_ID="$TriggerID >>scienceProducts.txt
		echo "#OBS_ID="$obsid >>scienceProducts.txt
		echo "#ORIGIN=CSC" >>scienceProducts.txt
		echo "#TRIG_MJD="$triggerJD >>scienceProducts.txt
		echo "#FILTER="$filter >>scienceProducts.txt
		echo "#RA_C="$RA >>scienceProducts.txt
		echo "#DEC_C="$DEC >>scienceProducts.txt
		echo "#TELESCOP="$telescopesvom >>scienceProducts.txt
		echo "#INSTRUME="$instrument >>scienceProducts.txt
		echo "#SOFT_VER=0.1" >>scienceProducts.txt
		echo "#-delta, exp, date-obs, filter, mag, merr, limitmag, ra, dec, xc, yc, distance, OTNAME, trigger_ID, image, telescope" >>scienceProducts.txt
		echo "#-delta, exp, date-obs, filter, mag, merr, limitmag, ra, dec, xc, yc, distance, IDCAND, BURST_ID(TOOMM_ID), TILENUM(default=0), IMAGEID, Telescope,fchart, alt_id, trig_name" >>scienceProducts.txt
		cat $OTfile | while read linesp
		do
			echo $linesp
			sourcename=`echo $linesp | awk '{print($13)}'`
			echo $sourcename
			fchart=`echo $sourcename $TriggerID $prefix | awk '{print($2"/"$3"_"$1"_cut.png")}'`
			fchart_batools=`echo $sourcename $TriggerID $prefix | awk '{print($3"_"$1"_cut.png")}'`
			fchart_batools_fit=`echo $sourcename $TriggerID $prefix | awk '{print($3"_"$1"_cut.fit")}'`
			echo $fchart $prefix 
	        	#ccdimgjpg=`echo $imsubname | sed 's/\.fit/.jpg/g'`
	
			echo $linesp $fchart $alt_id $trig_name >>scienceProducts.txt 
			echo $linesp $fchart_batools $alt_id $trig_name >>$OTfile_BAtools
			python3  ~/software/softgft/sendFile.py  cutimagepng  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $fchart_batools
			python3  ~/software/softgft/sendFile.py  cutimagefit  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $fchart_batools_fit

		done
	else
		echo "===========no ot==========="
		echo "#- delimiter is = , split parameter with value. " >scienceProducts.txt
                curTime=`date -u +%F"T"%T`
                echo "#CREATTIME="$curTime >>scienceProducts.txt
                echo "#FINDING=False" >>scienceProducts.txt
                echo "#UPMAG=$limitmag" >>scienceProducts.txt
                echo "#OBSTYPE="$obstype >>scienceProducts.txt
		echo "#BURST_ID: "$TriggerID >>scienceProducts.txt
		echo "#OBS_ID: "$obsid >>scienceProducts.txt
		echo "#ORIGIN: CSC" >>scienceProducts.txt
		echo "#TRIG_MJD: "$triggerJD >>scienceProducts.txt
		echo "#FILTER="$filter >>scienceProducts.txt
		echo "#RA_C="$RA >>scienceProducts.txt
		echo "#DEC_C="$DEC >>scienceProducts.txt
                echo "#TELESCOP="$telescopesvom >>scienceProducts.txt
                echo "#INSTRUME="$instrument >>scienceProducts.txt
                echo "#SOFT_VER=0.1" >>scienceProducts.txt
                echo "#-delta, exp, date-obs, filter, mag, merr, limitmag, ra, dec, xc, yc, distance, OTNAME, trigger_ID, image, telescope" >>scienceProducts.txt
                echo "#-delta, exp, date-obs, filter, mag, merr, limitmag, ra, dec, xc, yc, distance, IDCAND, BURST_ID(TOOMM_ID), TILENUM(default=0), IMAGEID, Telescope,fchart, alt_id, trig_name" >>scienceProducts.txt
		echo $DelTimeSec $exptime $dateobs $filter 999 999 $limitmag 999 999 999 999 999 999 $TriggerID 0  $prefix $telescope  999  $alt_id $trig_name  >>scienceProducts.txt
	fi
	mv scienceProducts.txt $OTfile		
	xsendCSC
	

}

xlimitgft ( )
{
	echo "xlimitgft"
	sn2=3.0
	#m1=smag  #magnitude of a bright source 
        gain=2.0
	#sn1=1/merr
	pi=3.1415926
	fwhm=2.0
	r1=`echo $fwhm | awk '{print($1*3.0)}'` #3fwhm
	r2=`echo $fwhm | awk '{print($1*1.0)}'` #1fwhm
	echo $sn2 $gain $pi $fwhm $r1 $r2
	if test -s matched.txt
	then
		head -1 matched.txt
		sum=0
		NcalJ=0
		cat matched.txt | awk '{if($6>0.05 && $6<0.2 && $10>5.0 )print($0)}' | head -5 > tmp
		mv tmp matched.txt
		wc matched.txt
		cat matched.txt | while read line
		do
			xc=`echo $line | awk '{print($1)}'`
			yc=`echo $line | awk '{print($2)}'`
			refxc=`echo $line | awk '{print($3)}'`
			refyc=`echo $line | awk '{print($4)}'`
			delr=`echo $line | awk '{print($5)}'`
			merr=`echo $line | awk '{print($6)}'`
			flux=`echo $line | awk '{print($7)}'`
			delmag=`echo $line | awk '{print($8)}'`
			instrmag=`echo $line | awk '{print($9)}'`
			refmag=`echo $line | awk '{print($10)}'`
			echo $xc $yc $refxc $refyc $delr $merr $flux $delmag $instrmag $refmag
			echo "merr=$merr"
			echo "flux=$flux"
			s1=`echo $flux $gain | awk '{print($1*$2)}'`
			echo "s1=$s1"
			sn1=`echo $merr | awk '{print(1.08/$1)}'`
			echo "sn1=$sn1"
			echo "pi=$pi r2=$r2 r1=$r1 s1=$s1 sn1=$sn1"
			#bb1=`echo $pi $r1 $s1 $sn1 | awk '{print(($3**2))}'
			#bb2=`echo $pi $r1 $s1 $sn1 | awk '{print(($4**2))}'
			#bb3=`echo $bb2 $s1 | awk '{print($1*$2)}'
			#bb4=`echo $bb1 $bb3 | awk '{print(($1-$2))}'
			#bb1=`echo $pi $r1 $s1 $sn1 | awk '{print(($3**2))}'
			n1=`echo $r1 $pi | awk '{print($2*$1*$1)}'`		
			n2=`echo $r2 $pi | awk '{print($2*$1*$1)}'`		
			echo "n1=$n1, n2=$n2"
			Nsky=`echo 10 | awk '{print(((s1*s1/sn1/sn1)-s1)/n1)}' s1=$s1 sn1=$sn1 n1=$n1`
			echo "sn2=$sn2 n2=$n2 Nsky=$Nsky"
			s2=`echo $sn2 $n2 $Nsky | awk '{print($1*($2*$3)**0.5)}'`
			echo "s2=$s2"
			echo "refmag=$refmag, s1=$s1, s2=$s2"
			#limitmag= mag + 2.5*log(s1/s2)
			limitmag=`echo $refmag $s1 $s2 | awk '{print($1+2.5*log($2/$3)/log(10))}'`   #3sigma
			echo "limitmag=$limitmag"
			sum1=`echo $sum $limitmag | awk '{print($1+$2)}'`
			sum=`echo $sum1 | awk '{print($1)}'`
			Ncal=`echo $NcalJ | awk '{print($1+1)}'`
			NcalJ=`echo $Ncal | awk '{print($1)}'`
			echo "sum=$sum NcalJ=$NcalJ"
			echo $sum $NcalJ >xlimit.txt
		done
		sum=`cat xlimit.txt | awk '{print($1)}'`
		NcalJ=`cat xlimit.txt | awk '{print($2)}'`
		echo $sum $NcalJ
		averagevalue=`echo $sum $NcalJ | awk '{print($1/$2)}'`
		limitmag=`echo $averagevalue`
		echo "limitmagnitude in 3 sigma is $averagevalue in $filter for $fitfile"
	else
		echo "There is no any matched.txt"
	fi	
}

xfluxcali ( )
{

echo "========xfluxcali========="
cat matched.txt
gnuplot << EOF
set term png
set output "$flux_calibration_png"
set xlabel "gft mag"
set ylabel "PS1 mag"
set grid
set key left
f(x)=a*x+b
a=1
fit f(x) 'matched.txt' u 9:10 via b  
plot 'matched.txt' u 9:10 t 'mag-mag',f(x)
quit
EOF

#in the gnuplot, a=1 is aiming to make the correct fit .
#cp fit.log fit.log.bak
#when fit above with only b
aa=1
bb=`cat fit.log | tail -1 | awk '{print($3)}'`
if [ "bb"x  == "data"x  ]
then
	bb=18.0
fi

#when fit above, via a,b,  the values of aa and bb are in the following
#aa=`cat fit.log | tail -9 | head -1 | awk '{print($3)}'`
#bb=`cat fit.log | tail -8 | head -1 | awk '{print($3)}'`
echo "the transformation format is f(x)="$aa"*x+"$bb
meandelmag=`echo $bb | awk '{print($1)}'`
echo "MeanDelMag=$meandelmag"

}


xOTsearch ( )
{
	echo "======xOTsearch===="
        cat PS1.txt | grep -v "ra" | tr ',' ' ' | grep -v "nan" | awk '{if($3>0)print($0)}' >tmp
        mv tmp PS1.txt
	head  PS1.txt
	cat PS1.txt | awk '{print($1,$2)}' > PS2.txt
        sky2xy $fitfile @PS2.txt >gaia.txt1 
	head gaia.txt1
	paste PS1.txt gaia.txt1 | grep -v "off" | awk '{print($8,$9,$3)}'>gaia.txt
	rm -rf PS2.txt newOT.txt matched.txt meandelmag.txt
	echo "============"
	head gaia.txt
	head -1 gaia.txt
	head -1 newobj.txt
	head -1 newobj_forOT.txt
	wc gaia.txt
	wc newobj_forOT.txt
        xcross30cmOTgft   #output files are newOT.txt
        #wc newOT.txt
	xcross30cmOTgft_mean  # matched.txt, meandelmag.txt
	wc matched.txt 
	#cat matched.txt
	wc meandelmag.txt
	#echo "meandelmag.txt"
	#cat meandelmag.txt
	head -1 newOT.txt
	if test -s meandelmag.txt
	then
		meandelmag=`tail -1 meandelmag.txt | awk '{print($1)}'`
		Nobj_mean=`tail -1 meandelmag.txt | awk '{print($3)}'`
		cp matched.txt matched.txt.bak
		#cat matched.txt | awk '{if($10<($9-meanvalue+1) && $10>($9-meanvalue-1))print($0)}' meanvalue=$meandelmag >tmp 
		#mv tmp matched.txt
		xfluxcali
	else
		echo "No meanvalue and Nobj_mean are derived"
		meandelmag=0
		Nobj_mean=0
	fi
	if test -s newOT.txt
	then
		#xfluxcalibration
		cat newOT.txt | awk '{print($1,$2,$3+meandelmag,$4,$5)}' meandelmag=$meandelmag | column -t >tmp
		mv tmp newOT.txt
		wc newOT.txt
        	xfilterpsf_OT
		wc newOT.txt
		xlimitgft
		echo "OK, please exit "
		
		#xcheckOT
	else
		xlimitgft
		echo "No any new sources are found in $fitfile"
	fi
	xOTInfo
	echo $Dir_result_trigger
	xcopyresult
}

xMkDirForTriggerID ( )
{
	Dir_current=`pwd`
	if test ! -d $Dir_result
	then
		mkdir $Dir_result
		cd $Dir_result
		if test ! -d $TriggerID
		then
			mkdir $TriggerID
		fi	
	fi	
	Dir_result_trigger=`echo $Dir_result $TriggerID | awk '{print($1"/"$2)}'`
	if test ! -d $Dir_result_trigger
	then
		cd  $Dir_result
		mkdir $TriggerID
	fi
	cd $Dir_current
}

xcopyresult ( )
{
	echo "=========xcopyresult======"
	Dir_current=`pwd`
	echo "$Dir_current"
	cd $Dir_current
	ls *.png
        cp *  $Dir_result_trigger
}

xGetcatalogWithFilter ( )
{
	echo "==xGetcatalogWithFilter=="
	echo "filter=$filter"
	if [ "$filter"x = "g"x  ]
	then
		echo "ggggg"
		cat PS1.txt | grep -v "Mag"  | tr ',' ' ' |  awk '{if($3>-2)print($1,$2,$3)}' | column -t >tmp
		mv tmp PS1.txt
#		 ~/anaconda3/bin/python ~/software/softgft/xGetPS1_gmag.py $RA $DEC 600.0  # in  arcsec to crossmatch the PS1 catalog and the OT list  
#		wait
	elif [ "$filter"x = "r"x  ] ||  [ "$filter"x = "R"x  ]
	then
		echo "rrrr or RRRR"
		cat PS1.txt |  grep -v "Mag"  | tr ',' ' ' | awk '{if($4>-2)print($1,$2,$4)}' | column -t >tmp
		mv tmp PS1.txt
		head -1 PS1.txt
		echo `pwd`
	#	 ~/anaconda3/bin/python ~/software/softgft/xGetPS1_rmag.py $RA $DEC 600.0  #in  arcsec to crossmatch the PS1 catalog and the OT list  
	#	wait
		
	elif [ "$filter"x = "i"x  ]  || [ "$filter"x = "I"x  ]
	then
		echo "iiii"
		cat PS1.txt |  grep -v "Mag"  | tr ',' ' ' | awk '{if($5>-2)print($1,$2,$5)}' | column -t >tmp
		mv tmp PS1.txt
	#	 ~/anaconda3/bin/python ~/software/softgft/xGetPS1_imag.py $RA $DEC 600.0  # in arcsec to crossmatch the PS1 catalog and the OT list  
	#	wait
	fi	

}

xcopyPS1ToDircatalog ( )
{
	TriggerIDtxt=`echo $TriggerID | awk '{print($1".txt")}'`
	cp PS1.txt $TriggerIDtxt
	cp $TriggerIDtxt $Dir_PS1catalog
	rm -rf $ToPS1catalog 
}
xPS1cross ( )
{
        echo "=====xPS1cross======"
	#to crossmatch with the PS1 catalog 
	PS1catalog=`echo $Dir_PS1catalog $TriggerID | awk '{print($1"/"$2".txt")}'`
	ToPS1catalog=`echo $Dir_PS1catalog $TriggerID | awk '{print($1"/"$2"_TogetCatalog.txt")}'`
	echo $PS1catalog $ToPS1catalog
	if test -s $PS1catalog
	then
		echo "have $PS1catalog in $Dir_PS1catalog"
		cp $PS1catalog PS1.txt
		rm -rf $ToPS1catalog
	else
		rm -rf PS1.txt
		echo "No $PS1catalog in $Dir_PS1catalog"
	        echo $RA $DEC
		echo `date`
		if test ! -r $ToPS1catalog  # it will be the first request for the field
		then
			echo $ToPS1catalog
			touch $ToPS1catalog 
			echo "to send the request for the PS1"
				
			echo " ~/anaconda3/bin/python ~/software/softgft/xGetPS1_grimag.py $RATT $DECTT $dis_PS1error"  
			#echo " ~/anaconda3/bin/python ~/software/softgft/xGetPS1_grimag.py $RA $DEC 600.0" >>xgetps1sh.cat
			#cp xgetps1sh.cat ../
			#200.228934, -27.042456
	       		#~/anaconda3/bin/python ~/software/softgft/xGetPS1_grimag.py $RATT $DECTT $dis_PS1error  # in  arcsec to crossmatch the PS1 catalog and the OT list  
	       		~/anaconda3/bin/python ~/software/softgft/xGetPS1_grimag.py $RATT $DECTT 1200  # in  arcsec to crossmatch the PS1 catalog and the OT list  
			wait
			
			
		else   #have send the request for the PS1 catalog, just wait
			echo "wait 2 sec for PS1 catalog"
			sleep 2
			xPS1cross
		fi
	fi
        if test -s PS1.txt
        then
		rm -rf $ToPS1catalog
		xcopyPS1ToDircatalog
		xGetcatalogWithFilter
		echo `date`
		xOTsearch
#		echo "=======Have got the PS1.txt, wll wait for 20 sec"
        else

		echo "No PS1.txt can be derived from PS1 catalog"
		#xUSNOcross   #not to be finished in this version.
		~/anaconda3/bin/python ~/software/softgft/xGetPS1_grimag.py $RATT $DECTT $dis_PS1error
		wait
		if test -s PS1.txt
		then
			xcopyPS1ToDircatalog
			xGetcatalogWithFilter
			xOTsearch
		else
			echo "No PS1.txt can be derived from PS1 catalog for 2nd"
			rm -rf $ToPS1catalog
			msg="No_PS1catalog__give_up"
			#xSend2db $msg			
			exit 0
		fi
	fi
}

xUSNO2SDSS ( )
{
	echo "=======xUSNO2SDSS========"
}

xUSNOCross ( )
{
	echo "========xUSNOCross======"
	xdefineForCatalog
	xGetCatalog
	xsky2xyTemp
	xOTsearch

}


xdefineForCatalog (  )
{
#    echo `pwd`
    #dir_getCatalog=$HOME/han/catalogue
    dir_getCatalog=$HOME/han/catalogue_v1.2

    RadiusTemp=0.6   #degree for usno
    lowMagTemp=-10.0    #brightest object
    largerMagTemp=23.0  #dimest object
    dir_usnob10=$HOME/usnob10
}


xCallimit ( )
{
        echo "xCalilimit"
        limit_sigma=3.0
        Gain=2.0   #e-/ADU
        Readnoise=8.0 #e-
	echo "@@@@@@@@@@@@@@@@"
	cat $OUTPUT_limit_new
	echo "@@@@@@@@@@@@@@@@@"
        #mag_check=`cat $OUTPUT_limit_new | awk '{if($17=="CHECK")print($8)}'`
	mag_check=`cat $OUTPUT_limit_new | awk '{print($3)}'`
        flux_check=`cat $OUTPUT_limit_new | awk '{print($10)}'`
        threshold=`cat $OUTPUT_limit_new | awk '{print($11)}'`
        sigma_sky=`echo $threshold $Detect_sigma | awk '{print($1/$2)}'`
        sky_check=`echo $sigma_sky | awk '{print($1*$1)}'`
        #photometry radius=8 so area=3.1415926*8*8 pixel
        phot_area=`echo 3.1415926 8 | awk '{print($1*$2*$2)}'`
        #noise_faint_object=`echo $phot_area $sky_check $Gain $Readnoise | awk '{print($1*($2*$3+$4*$4))}'`
        noise_faint_object=`echo $phot_area $sky_check $Gain | awk '{print($1*$2*$3)}'`
        #x0=(-b+/-(b^2-4ac))/2a  
        flux_faint_object=`echo $noise_faint_object $limit_sigma | awk '{print(($2*$2+($2**(4)+4*$1*$2*$2)**(0.5))/2)}'`
        limit_mag=`echo $mag_check  $flux_check $flux_faint_object | awk '{printf("%.2f\n",$1+2.5*log($2/$3)/log(10))}'`
        echo "limit_sigma=$limit_sigma Gain=$Gain Readnoise=$Readnoise flux_check=$flux_check sky_check=$sky_check mag_check=$mag_check phot_area=$phot_area noise_faint_object=$noise_faint_object flux_faint_object=$flux_faint_object limit_mag=$limit_mag"
        echo "limit_mag=$limit_mag"
	sethead -kr X LIMITMAG=$limit_mag   $fitfile
	mv $fitfile ../
	cd ../
	rm -rf reduc

}

xGetCatalog (  )
{
   echo "xGetCatalog"
    cd $Dir_now
#    echo "~/usnob10/src/usnob1 -R $dir_usnob10 -c $RA $DEC  -r 25 -lmr2 0,23 -m 2000000 -sr >listusnobobj"
    echo "~/usnob10/src/usnob1 -R $dir_usnob10 -c $RA $DEC  -r 25 -lmr2 0,23 -m 2000000 -sr"
    ~/usnob10/src/usnob1 -R $dir_usnob10 -c $RA $DEC  -r 150 -lmr2 0,18 -m 2000000 -sr >listusnobobj

    python $dir_usnob10/xusnob10_format.py ./ listusnobobj
    echo "deltayear=$deltayear"
    #msec --->deg    /3600/1000
    #deg--->rad
    echo "=========tmp_listusnobobj====="
    head -1 tmp_listusnobobj
    cat tmp_listusnobobj | awk '{print($1+$7*deltayear/cos($2*3.1415926/180)/3600/1000,$2+$8*deltayear/3600/1000,$3,$4,$5,$6)}' deltayear=$deltayear > $TempUSNORADEC
    #cat tmp_listusnobobj | awk '{print($1,$2,$3,$4,$5,$6)}' deltayear=$deltayear > $TempUSNORADEC
    cp tmp_listusnobobj aold.txt
    #output includes ra dec b1 b2 R1 R2 I

#    ~/usnob1/usnob1 -m 2000000  -r 0.3 -c $RA $DEC -lmr -6,20 -sr > $TempUSNORADEC
    if test -s $TempUSNORADEC
    then
        echo "Geting catalog is successful"
        wc $TempUSNORADEC
    else
        echo "Geting catalog failed"
        echo $Dir_now
        exit 0
    fi
}
xdefineForFits (  )
{
echo "=========xdefineForFits============"
#    echo `pwd`

    AstroCaliFile=`echo $fitfile | sed 's/\.fit/.new/g'`
    OUTPUT=`echo $fitfile | sed 's/\.fit/.sex/g'`
    OUTPUT_limit=`echo $fitfile | sed 's/\.fit/.sex_limit/g'`
    OUTPUT_limit_new=`echo $fitfile | sed 's/\.fit/.sex_new_limit/g'`
    OTfinalres_limit=`echo $fitfile | sed 's/\.fit/_otfinalres_withlimit.txt/g'`
    prefix=`echo $fitfile | sed 's/\.fit//g'`
    OUTPUT_new=`echo $fitfile | sed 's/\.fit/.sexnew/g'`
    TempUSNORADEC=`echo $fitfile | sed 's/\.fit/_tempRADEC.txt/g'`
    TempUSNOXY=`echo $fitfile | sed 's/\.fit/_tempXY.txt/g'`
    otxy=`echo $fitfile | sed 's/\.fit/_otxy.txt/g'`
    MonitorFile=`echo $fitfile | sed 's/\.fit/_res.txt/g'`
    matchedfile=`echo $fitfile | sed 's/\.fit/_KnownObjxy.txt/g'`
    compstarsfile=`echo $fitfile | sed 's/\.fit/_compstars.txt/g'`
    OTfile=`echo $fitfile | sed 's/\.fit/_forCSC.txt/g'`
    OTfile_BAtools=`echo $fitfile | sed 's/\.fit/_otrealxy_BAtools.txt/g'`
    checkstarfile=`echo $fitfile | sed 's/\.fit/_checkstarrealxy.txt/g'`
    deltaMagfile=`echo $fitfile | sed 's/\.fit/_deltaMag.txt/g'`
    OTfinalres_withdis=`echo $fitfile | sed 's/\.fit/_otfinalres_withdis.txt/g'`
    OTfinalres=`echo $fitfile | sed 's/\.fit/_otfinalres.txt/g'` 
    OTfinalresDiscal=`echo $fitfile | sed 's/\.fit/_otfinalresDisCal.txt/g'`
    OTFieldxyForTvMark=`echo $fitfile | sed 's/\.fit/_otxyfortvmark.txt/g'`
    OTFieldxy=`echo $OTid | awk '{print($1".otxy")}'`
    OTTriggerxy=`echo $OTid | awk '{print($1"_trigger.otxy")}'`
    OTFieldxy_ref=`echo $OTid | awk '{print($1".otxyref")}'`
    OTFieldTemp=`echo $OTid | awk '{print($1".tempcomp")}'`
    OTFieldUSNO=`echo $OTid | awk '{print($1".tempusno")}'`
    OTWCSfit=`echo $OTid | awk '{print($1"_wcs.new.fit")}'`
    GRB_Total_log=`echo $OTid  | awk '{print($1"_Total.log")}'`   #only for the recond for all the images which have been processed.
    GRBlog=`echo $OTid  | awk '{print($1"_all.log")}'`            #only for the image combiniation.
    GRBcandidatelog=`echo $OTid | awk '{print($1"_candidate.log")}'`	 #candidates list	 
    otlog=`echo $fitfile | sed 's/\.fit/_process.log/g'`   # to recond the result of the data process for each image.  
    flux_calibration_png=`echo $fitfile | sed 's/\.fit/.fit.fluxcali.png/'`
}

xfilterObjInnerError ( )
{
	echo "xfilterObjInnerError"
	xy2sky -d $fitfile @newobj.txt >newobj_sky.txt
        #to calculate the distance of all new sources to the center of the burst
        rm -rf disr.txt
        paste newobj.txt newobj_sky.txt | column -t >newobj_xysky.txt     # the log file for candidates for one images
        rm -rf tmpOTfile disr.txt
        cat newobj_xysky.txt | while read linexysky
        do
#                echo $linexysky
                RA1=`echo $linexysky | awk '{print($6)}'`
                DEC1=`echo $linexysky | awk '{print($7)}'`
                disr=`skycoor -r $RA1 $DEC1 $RATT $DECTT | awk '{print($1)}'`  #arcseconds
                #disr is the distance for this new source from the center of the alert in arcseconds.
                if [ ` echo " $disr < $errorp " | bc ` -eq 1  ]
                then
			echo $linexysky | awk '{print($1,$2,$3,$4,$5)}' >>newobj_xysky.txt1
                fi
        done
	mv newobj_xysky.txt1 newobj_forOT.txt
	wc newobj_forOT.txt
	rm -rf newobj_sky.txt newobj_xysky.txt 

}

xGetstars (  )
{
	echo "===========xGetstars=========="
    rm -rf newobj.txt
    echo `pwd`
    echo $fitfile
    Detect_sigma=1.5
    if [ "$filter"x == "i"x  ]
    then
	Detect_sigma=1.5
    fi
    echo "Detect_sigma=$Detect_sigma"
    sex $fitfile  -c  daofind.sex  -DETECT_THRESH $Detect_sigma -ANALYSIS_THRESH $Detect_sigma -CATALOG_NAME $OUTPUT
    cp $OUTPUT $OUTPUT_limit
    cat $OUTPUT | awk '{if($1>ejmin && $2>ejmin && $1<ejmax && $2<ejmax && $4<0.3 && $7>2.0)print($0)}' ejmin=$ejmin  ejmax=$ejmax | sort -n -k 3  >temp
    mv temp $OUTPUT
    cat $OUTPUT | awk '{if($12>fluxmax)print($0)}' fluxmax=$fluxmax_adu >brightsource.txt
    if test -s brightsource.txt
    then
	echo " to filter the sources caused by smear effect by the satuated objects"
    	cat brightsource.txt | while read lineb
    	do
       	xc=`echo $lineb | awk '{print($1)}'`
       	yc=`echo $lineb | awk '{print($2)}'`
       	echo $xc $yc	
       	cat  $OUTPUT | awk '{if($1<(xc-2) || $1>(xc+2))print($0)}' xc=$xc yc=yc >temp
	echo $lineb >>temp
       	mv temp $OUTPUT
    	done
    fi
    cat  $OUTPUT | awk '{print($1,$2,$3,$4,$10)}' > newobj.txt
    wc newobj.txt
    #in newobj.txt, x,y,mag,merr,flux
    Nobj=`wc -l $OUTPUT | awk '{print($1)}'`
    if [ $Nobj -gt $Nobj_least  ]
    then
        echo "$fitfile has $Nobj stars "
        cp $OUTPUT $OUTPUT_new
    else
        echo "Only $Nobj stars in the $fitfile, please try another image or increase the exptime"
    #    xdisplayOTandnewImg
        echo $Dir_now
	msg="Only_"$Nobj"_stars_in_the_"$fitfile"__give_up"
	#xSend2db $msg
	exit 0
    #    SendMessage=`echo "Error_Few_of_objects:W60ccd_"$fitfile"_please_increase_the_exptime"`
    #    xSendmsg2Wechat12 $SendMessage
    fi
  #cat newobj.txt
   xfilterObjInnerError
}



xsky2xyTemp(  )
{
    rm tempxy
   # sky2xy $AstroCaliFile @$TempUSNORADEC >tempxy
    sky2xy $fitfile @$TempUSNORADEC >tempxy
        wc tempxy
   echo "$TempUSNORADEC"  
   paste tempxy $TempUSNORADEC | grep -v "off" | awk '{if($5>0 && $5<CCDsize && $6>0 && $6<CCDsize)print($5,$6,$7,$8,$9,$10,$11,$12,"99")}' CCDsize=$CCDsize >$TempUSNOXY   #Depend on the parameters after the xGetkeywords. The final parameters are:    x,y, ra, dec, B1, B2,R1,R2,99
    xUSNO2SDSS
    if test -s $TempUSNOXY
    then
        echo "xsky2xy is finished"
    else
        echo "No any source after the xsky2xy"
        echo $Dir_now
        exit 0
    fi
}

xAstronomy ( )
{
xastro_30cm_WithRaDecCenter.sh $fitfile

}

xastronomy (  )
{
#    echo "To do the xastronomy"
#    echo `pwd`
     echo $fitfile
        echo `pwd`
    rm -rf *.axy *objs-png

#    solve-field --nsigma 5 --use-sextractor -scale -H 0.7 -L 0.2 -u app -3 $RA -4 $DEC -5 $SolveRadius $fitfile   
    echo "solve-field -O -t 1 --use-sextractor --nsigma 5 -scale -H $scaleH -L $scaleL -u app -3 $RA -4 $DEC -5 $SolveRadius $fitfile" 
    solve-field -O -t 1 --use-sextractor --nsigma 5 -scale -H $scaleH -L $scaleL -u app -3 $RA -4 $DEC -5 $SolveRadius $fitfile 
    wait
    if test -r $AstroCaliFile
    then
       echo "AStronomy calibration is successful"
       cp $AstroCaliFile $OTWCSfit
       cp $OTWCSfit $fieldAstrometryCaliFile
    else
        echo "Astronomy calibration failed, try another image please"
        echo $Dir_now
        exit 0
    fi

}

xmkproduction_rawimagepng ( )
{
	echo "make rawimage and rawimagepng for BA tools"
	sky2xy $fitfile $RATT $DECTT >tmp
	alert_xc=`cat tmp | awk '{print($5)}'`
	alert_yc=`cat tmp | awk '{print($6)}'`
	rm -rf tmp
	errorparcmin=`echo $errorp | awk '{print($1/60)}'`
	echo "/home/cgft/anaconda3/bin/python  ~/software/softgft/xReadFitsWithWCS3_wholeimage.py $fitfile 1 $NAXIS1 1 $NAXIS2 $TriggerID $alert_xc $alert_yc $errorparcmin $dateobs $telescope $filter"
	/home/cgft/anaconda3/bin/python  ~/software/softgft/xReadFitsWithWCS3_wholeimage.py $fitfile 1 $NAXIS1 1 $NAXIS2 $TriggerID $alert_xc $alert_yc $errorparcmin $dateobs $telescope $filter
	wait
	
	#rawimagepng=`echo $prefix $TriggerID | awk '{print($1"_"$2".png")}'`
	rawimagepng=`echo $prefix  | awk '{print($1".png")}'`
	echo $rawimagepng
	if test -r $rawimagepng
	then
		msg=`echo $fitfile"_fit_to_png_successful"`
		#xSend2db $msg	
		echo "python3  ~/software/softgft/sendFile.py  rawimagepng  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $rawimagepng"
		python3  ~/software/softgft/sendFile.py  rawimagepng  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $rawimagepng
		msg=`echo $rawimagepng"_has_uploaded"`

	else
		msg=`echo $fitfile"_fit_to_png:_failed"`
		#xSend2db $msg
	fi	      	
        python3  ~/software/softgft/sendFile.py  rawimagefit  $TriggerID  001 002  $telescope  $dateobs  $telescope   pipeline  01 test1 $fitfile
	wait
	msg=`echo $fitfile"_has_uploaded"`
	#xSend2db $msg

}

xastronomy_2 ( )
{
    echo "xastronomy_others"
    echo $fitfile

    echo "RATT=$RATT DECTT=$DECTT SolveRadiusT=$SolveRadiusT fitfile=$fitfile "	
    echo "solve-field -O --use-sextractor --nsigma 5 -scale -H 0.68 -L 0.60 -u app -3 $RATT -4 $DECTT -5 $SolveRadiusT $fitfile"
	
    solve-field -O -t 1 -p --use-sextractor -scale -H 0.68 -L 0.50 -u app -3 $RATT -4 $DECTT -5 $SolveRadiusT $fitfile
    #solve-field -O -p --use-sextractor -scale -H 0.68 -L 0.60 -u app -3 $RATT -4 $DECTT -5 $SolveRadiusT $fitfile
    wait
    if test -r $AstroCaliFile
    then
        rm -rf $fitfile
        cp $AstroCaliFile $fitfile
    else
	echo "Astronomy calibration failed, try another image please"
        echo $Dir_now
        exit 0

    fi
}


xselectobj ( )
{
	echo "xselectobj"
	cat $OUTPUT
	echo "====================="
	cat $OUTPUT | while read line 
	do
		echo $line
		xc=`echo $line | awk '{print($1)}'`
		yc=`echo $line | awk '{print($2)}'`
		mag_instr=`echo $line | awk '{print($3)}'`
		cat $TempUSNOXY | awk '{if(($1-xc)*($1-xc)< 4.0 && ($2-yc)*($2-yc)< 4.0)print($0)}' xc=$xc yc=$yc >tempusno
		if test -s tempusno
		then
			cat tempusno
			echo "!!!!!!!!!!!!!!"
			filter=`gethead $fitfile "filter"`
			echo "filter=$filter"
			xfluxcali=`head -1 tempusno | awk '{print($1)}'`
			yfluxcali=`head -1 tempusno | awk '{print($2)}'`
			if [ "$filter"x = "R"x  ]  || [ "$filter"x = "Lum"x ]
			then
				mag2=`head -1 tempusno | awk '{print($8)}'`
			elif [ "$filter"x  = "B"x ]
			then
				 mag2=`head -1 tempusno | awk '{print($6)}'`	
			else
				mag2=20
			fi
			echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
			echo $line $mag2
			DeltaMagCali=`echo $mag_instr $mag2 | awk '{print($1-$2)}'`
			echo "~~~~~~~~~~~~~~~~~~~"
			echo $line $mag2 | awk '{print($1,$2,$13,$4,$5,$6,$7,$8,$9,$10,$11,$12)}' >$OUTPUT_limit_new
		        sethead -kr X xfluxcali=$xfluxcali  yfluxcali=$yfluxcali MagInstr=$mag_instr USNOMag=$mag2 DeltaMagCali=$DeltaMagCali $fitfile				
			break
		else
			echo "no tempusno"
		fi	
	done
	cat $OUTPUT_limit_new
}

xmkQLC ( )
{
	echo "=========xmkQLC====="
	firstimg=`ls *.fit | head -1`
	echo "This part shall be updated based on the new interface of the image header"
	DATE_OBS=`gethead $firstimg "date-obs" | awk '{print("\""$1"\"")}'`
        TRIG_MJD=`gethead $firstimg "TRIG_JD"` 
	BURST_ID=`gethead $firstimg "BURST_ID" | awk '{print("\""$1"\"")}'`
	OBS_ID=`gethead $firstimg "OBS_ID" | awk '{print("\""$1"\"")}'`
	#- fill data
	cat *.lc >listlc
	lcfile=listlc	
        DELTAT_list=`cat $lcfile | awk '{print($1)}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        EXPTIME_list=`cat $lcfile | awk '{print($2)}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        TSTART_list=`cat $lcfile | awk '{print("\""$3"\"")}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        FILTER_list=`cat $lcfile | awk '{print("\""$4"\"")}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        MAG_list=`cat $lcfile | awk '{print($5)}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        MAGERR_list=`cat $lcfile | awk '{print($6)}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        IDCAND_list=`cat $lcfile | awk '{print("\""$13"\"")}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        IMAGEID_list=`cat $lcfile | awk '{print("\""$15"\"")}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        GFTTGID_list=`cat $lcfile | awk '{print("\""$14"\"")}' | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        SUBIMAGE_list=`cat $lcfile | awk '{print("\""$15"_"$13"_cut.png""\"")}' | sed 's/.fit//g'  | tr '\n' ',' | sed 's/.$//' | awk '{print("["$0"]")}'`
        echo $SUBIMAGE_list
        echo $DELTAT_list 
        echo "T.PDX.HDU2.TABLE.DELTAT.value=$DELTAT_list"
        echo "T.PDX.HDU2.TABLE.EXPTIME.value=$EXPTIME_list"
        echo "T.PDX.HDU2.TABLE.TSTART.value=$TSTART_list"
        echo "T.PDX.HDU2.TABLE.FILTER.value=$FILTER_list"
        echo "T.PDX.HDU2.TABLE.MAG.value=$MAG_list"
        echo "T.PDX.HDU2.TABLE.MAGERR.value=$MAGERR_list"
        echo "T.PDX.HDU2.TABLE.IDCAND.value=$IDCAND_list"
        echo "T.PDX.HDU2.TABLE.IMAGEID.value=$IMAGEID_list"
        echo "T.PDX.HDU2.TABLE.GFT_TGID.value=$GFTTGID_list"
        echo "T.PDX.HDU2.TABLE.SUBIMAGE.value=$SUBIMAGE_list"
        echo "T.PDX.HDU2.HEADER.DATE___OBS.value=$DATE_OBS"
        echo "T.PDX.HDU2.HEADER.TRIG_MJD.value=$TRIG_MJD"

        echo "T.PDX.HDU2.TABLE.DELTAT.value=$DELTAT_list" >xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.EXPTIME.value=$EXPTIME_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.TSTART.value=$TSTART_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.FILTER.value=$FILTER_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.MAG.value=$MAG_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.MAGERR.value=$MAGERR_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.IDCAND.value=$IDCAND_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.IMAGEID.value=$IMAGEID_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.GFT_TGID.value=$GFTTGID_list" >>xgftMkProducts_main.cat
        echo "T.PDX.HDU2.TABLE.SUBIMAGE.value=$SUBIMAGE_list" >>xgftMkProducts_main.cat


	Dir_software="~/software/softgft"
	xMkHeadForQLC
	xMkTailForQLC
	cat xgftMkProducts_head.cat xgftMkProducts_main.cat xgftMkProducts_tail.cat >xgftMkProducts.py
	#QLC_GFT_CGFT_test.fits
	if test -r QLC*.fits
	then
		rm -rf QLC*.fits
	fi
	chmod +x xgftMkProducts.py
	./xgftMkProducts.py
	if test -s QLC*.fits
	then
		ls QLC*.fits
		echo "QLC*.fits is successful"
		xCopyResult2DB
	fi


}

xMkHeadForQLC ( )
{

headfile=xgftMkProducts_head.cat

echo "#!/usr/bin/env python" > $headfile
#======================================
#- run_test.py
#- to read fits template and fill data.
#- 

echo "import jsfits" >>$headfile

#- read temp
echo "T=jsfits.PDFits()" >>$headfile
echo "T.readfits(\"QLC_GFT_CGFT.fits\")" >>$headfile
echo "T.PDX.HDU1.HEADER.CARD.value='QLC_GFT_CGFT'" >>$headfile     
echo "T.PDX.HDU2.HEADER.DATE___OBS.value=$DATE_OBS" >>$headfile
echo "T.PDX.HDU2.HEADER.TRIG_MJD.value=$TRIG_MJD" >> $headfile		
echo "T.PDX.HDU2.HEADER.BURST_ID.value=$BURST_ID" >> $headfile
echo "T.PDX.HDU2.HEADER.OBS_ID.value=$OBS_ID" >> $headfile				

}

xMkTailForQLC ( )
{

headfile=xgftMkProducts_tail.cat
#- have to use list insead of array for table.
#- output to fits file.
echo "T.writeToFITS('QLC_GFT_CGFT_test.fits')" >$headfile

}

xCopyResult2DB ( )
{
	echo "=======xCopyResult2DB======"
	echo "Waiting for the interface to Wuchao for furhter coding"
	:
}

xmkproducts ( )
{
	Dir_be=`pwd`
	cd $Dir_result_trigger 
	xmkQLC
	msg="To_generate_the_QLC_products"
	#xSend2db $msg
	#other products

	cd $Dir_be
}

xGetProducts ( )
{
	echo "xGetProducts"
	#get the current time
	#calcuate the delay time from the trigger time to the current time
	# if the delay time is larger than 4 min, then run this function.
	#
	if [ ` echo " $DelTimeSec > $DeltaTime_p " | bc ` -eq 1  ]
	then
		if test -s $SPTimelog
		then
			echo "This is not the first time"
			xcheckTimeDelay
		else
			echo "This is the first time to produce the SVOM Products"
			xmkproducts
			date +%s >$SPTimelog
			cat $SPTimelog
		fi
	else
		echo "The delay time for the data time from trigger time is $DelTimeSec, which is not longer than the $DeltaTime_p"	
	fi



}

xcheckTimeDelay ( )
{
	echo "===xcheckTimeDelay====="
	time1=`cat $SPTimelog | awk '{print($1)}'`
	DelayTimeSec=`date +%s | awk '{print($1-time1)}' time1=$time1  `
	echo "DelayTimeSec=$DelayTimeSec"
	if [ ` echo " $DelayTimeSec > $DelayTime_UpdateSP " | bc ` -eq 1  ]
	then
		echo "Have to generate the SP again"
		date +%s >$SPTimelog
		xmkproducts
	else
		echo "The delay time is only $DelayTimeSec. Waiting for more data"
	fi
}

xcopy
xGetkeywords_1
xdefineForFits
msg="astronometric_calibration"
#xSend2db $msg
xastronomy_2
msg="Get_keywords_from_image"
#xSend2db $msg
xGetkeywords
#xastronomy_others
xmkproduction_rawimagepng
msg="Get_source_list"
#xSend2db $msg
xGetstars
msg="Search_optical_transients_with_PS1_catalog"
#xSend2db $msg
xPS1cross
msg="over"
#xSend2db $msg

#xGetProducts

echo "===over==="
sleep 1
